package com.caishi.lkx.exam.ienum.type;

import com.zzw.common.mysql.MybatisPlusEnum;
import lombok.Getter;

import java.util.Arrays;

/**
 * @author by keray
 * date:2019/8/3 14:30
 */
public enum PaperType implements MybatisPlusEnum<Integer> {
    //
    prophesy("冲刺密卷", 4),
    mock("模拟试卷", 2),
    trueTopic("历年真题", 1),
    jingxuansj("精选试卷", 3),
    qianghuaxunlian("强化训练", 5),
    yatimijuan("押题密卷", 6),
    gaopinkj("高频考卷", 7),
    zhongdiansj("重点试卷", 8),
    tifensj("提分试卷", 9);



    @Getter
    final String desc;

    @Getter
    final Integer value;

    @Override
    public Integer getCode() {
        return value;
    }

    PaperType(String desc, Integer value) {
        this.desc = desc;
        this.value = value;
    }

    public static PaperType int2Obj(int value) {
        return Arrays.stream(PaperType.values())
                .filter(v -> v.value == value).findFirst().orElse(null);
    }

    public static PaperType string2Obj(String value) {
        return Arrays.stream(PaperType.values())
                .filter(v -> v.getDesc().equals(value)).findFirst().orElse(null);
    }

    public static int array2Int(PaperType[] userTypes) {
        int result = 0;
        for (PaperType l : userTypes) {
            result |= l.getValue();
        }
        return result;
    }

    public static PaperType[] int2Array(int value) {
        return Arrays.stream(PaperType.values())
                .filter(v -> (v.getValue() & value) == v.getValue()).toArray(PaperType[]::new);
    }

    public static boolean intHaveType(int value, PaperType level) {
        return (value & level.value) == level.value;
    }

    public static boolean intAllHaveType(int value, PaperType[] levels) {
        return Arrays.stream(levels).allMatch(v -> (v.getValue() & value) == v.getValue());
    }

    public static boolean intAnyHave(int value, PaperType[] levels) {
        return Arrays.stream(levels).anyMatch(v -> (v.getValue() & value) == v.getValue());
    }

}
